package cn.wsn2018.tools.ctrl;

import cn.wsn2018.tools.common.CommonValue;
import cn.wsn2018.tools.service.UserService;
import cn.wsn2018.tools.util.ServletUtil;
import org.apache.log4j.Logger;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


@RestController
public class UserController {
    @Resource
    private UserService userService;
    private Logger logger=Logger.getLogger(UserController.class);

    @RequestMapping(CommonValue.REQUEST_MAPPING_LOGIN)
    /**
     * 处理前端登录表单，前端填写用户名密码经过验证之后返回登录状态的json字符串
     */
    public void login(HttpServletRequest request, HttpServletResponse response)throws Exception{
        ServletUtil.getInstance().requestOperating(request);
        String username=request.getParameter(CommonValue.REQUEST_PARAM_USERNAME);
        String password=request.getParameter(CommonValue.REQUEST_PARAM_PASSWORD);
        logger.debug(username+":"+password);
        String json=userService.dealUserLogin(username,password);
        ServletUtil.getInstance().responseOperating(response, json);
    }

    @RequestMapping(CommonValue.REQUEST_MAPPING_REGIST)
    /**
     * 处理前端注册表单，前端填写用户名密码经过验证之后返回注册状态的json字符串
     */
    public void regist(HttpServletRequest request, HttpServletResponse response)throws Exception{
        ServletUtil.getInstance().requestOperating(request);
        String username=request.getParameter(CommonValue.REQUEST_PARAM_USERNAME);
        String password=request.getParameter(CommonValue.REQUEST_PARAM_PASSWORD);
        String json=userService.dealUserRegist(username,password);
        ServletUtil.getInstance().responseOperating(response, json);
    }
}
